package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.modular.crm.entity.GwggT;
import cn.e4j.modular.crm.entity.GwghT;
import cn.e4j.modular.crm.service.GwggTService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 竞争信息
 * Created by miaoyl on 2019-1-11.
 */
@Controller
@RequestMapping("/crm/gwgi004")
public class Gwgi004Controller extends BaseController {
    @Autowired
    private GwggTService gwggTService;

    /**
     * 跳转投标信息申请管理页面
     */
    @RequiresPermissions("crm:gwgi004:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwgi004.html";
    }

    /**
     * 查询竞争分析列表
     */
    @RequestMapping("/getGwgglist")
    @RequiresPermissions("crm:gwgi004:init")
    @ResponseBody
    public ResponseData getGwgclist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = this.gwggTService.getGwggList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 获取新增默认值
     */
    @RequestMapping("/getAddData")
    @ResponseBody
    public ResponseData getAddData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto addDataDto = gwggTService.getAddData(inDto);
        return new SuccessResponseData(addDataDto);
    }

    /**
     * 新增
     */
    @RequestMapping("/addData")
    @RequiresPermissions("crm:gwgi004:add")
    @ResponseBody
    public ResponseData addData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        List<GwghT> gwghTList = JSONObject.parseArray(jsonObject.getString("gwghList"), GwghT.class);
        GwggT gwggT = JSONObject.parseObject(jsonObject.getString("gwggData"), GwggT.class);
        gwggTService.addData(gwghTList, gwggT);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/editData")
    @RequiresPermissions("crm:gwgi004:edit")
    @ResponseBody
    public ResponseData editData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        List<GwghT> gwghTList = JSONObject.parseArray(jsonObject.getString("gwghList"), GwghT.class);
        GwggT gwggT = JSONObject.parseObject(jsonObject.getString("gwggData"), GwggT.class);
        gwggTService.editData(gwghTList, gwggT);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwgi004:del")
    @ResponseBody
    public ResponseData delete(GwggT gwggT) {
        gwggTService.deleteData(gwggT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/delDataBatch")
    @RequiresPermissions("crm:gwgi004:del")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwggT> gwggTList = inDto.getReqList(GwggT.class);
        gwggTService.delDataBatch(gwggTList);
        return SUCCESS_TIP;
    }

    /**
     * 根据招标编号查询GWGH数据
     */
    @RequestMapping("/getTable")
    @ResponseBody
    public ResponseData getTable(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> gwghList = this.gwggTService.getGwghData(inDto);
        return ResponseData.success(gwghList);
    }

    /**
     * 修改状态
     */
    @RequestMapping("/updateStatus")
    @RequiresPermissions("crm:gwgi004:init")
    @ResponseBody
    public ResponseData updateStatus(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwggT> gwggTList = inDto.getReqList(GwggT.class);
        gwggTService.updateStatus(gwggTList);
        return SUCCESS_TIP;
    }

    /**
     * 查询投标信息
     */
    @RequestMapping("/getGwgdData")
    @ResponseBody
    public ResponseData getGwgdList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = this.gwggTService.getGwgdData(inDto);
        return ResponseData.success(result);
    }

}
